﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.CONTACT.Components
{
    public partial class Item
    {
        #region Private members.

        protected int _ID;
        protected int _ModuleID;
        protected string _Name = string.Empty;
        protected string _Role = string.Empty;
        protected string _Email = string.Empty;
        protected string _Contact1 = string.Empty;
        protected string _Contact2 = string.Empty;
        protected string _Fax = string.Empty;
        protected string _Address = string.Empty;
        protected string _CreatedBy = string.Empty;
        protected DateTime _CreationTime;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int ModuleID
        {
            set { this._ModuleID = value; }
            get { return this._ModuleID; }
        }

        public string Name
        {
            set { this._Name = value; }
            get { return this._Name; }
        }

        public string Role
        {
            set { this._Role = value; }
            get { return this._Role; }
        }

        public string Email
        {
            set { this._Email = value; }
            get { return this._Email; }
        }

        public string Contact1
        {
            set { this._Contact1 = value; }
            get { return this._Contact1; }
        }

        public string Contact2
        {
            set { this._Contact2 = value; }
            get { return this._Contact2; }
        }

        public string Fax
        {
            set { this._Fax = value; }
            get { return this._Fax; }
        }

        public string Address
        {
            set { this._Address = value; }
            get { return this._Address; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }

        public DateTime CreationTime
        {
            set { this._CreationTime = value; }
            get { return this._CreationTime; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Item Load(int iD)
        {
            string spName = "[dbo].p_CONTACT_Item_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Item entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetInt32(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
                if (!reader.IsDBNull(reader.GetOrdinal("Role"))) entity.Role = reader.GetString(reader.GetOrdinal("Role"));
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
                if (!reader.IsDBNull(reader.GetOrdinal("Contact1"))) entity.Contact1 = reader.GetString(reader.GetOrdinal("Contact1"));
                if (!reader.IsDBNull(reader.GetOrdinal("Contact2"))) entity.Contact2 = reader.GetString(reader.GetOrdinal("Contact2"));
                if (!reader.IsDBNull(reader.GetOrdinal("Fax"))) entity.Fax = reader.GetString(reader.GetOrdinal("Fax"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) entity.Address = reader.GetString(reader.GetOrdinal("Address"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreationTime"))) entity.CreationTime = reader.GetDateTime(reader.GetOrdinal("CreationTime"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------


        public static DataSet SelectAll(int moduleID)
        {
            string spName = "[dbo].p_CONTACT_Item_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll(int moduleID)
        {
            string spName = "[dbo].p_CONTACT_Item_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, moduleID);
            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_CONTACT_Item_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_CONTACT_Item_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int moduleID, string name, string role, string email, string contact1, string contact2, string fax, string address, string createdBy, DateTime creationTime)
        {
            Item entity = new Item();
            entity.ModuleID = moduleID;
            entity.Name = name;
            entity.Role = role;
            entity.Email = email;
            entity.Contact1 = contact1;
            entity.Contact2 = contact2;
            entity.Fax = fax;
            entity.Address = address;
            entity.CreatedBy = createdBy;
            entity.CreationTime = creationTime;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_CONTACT_Item_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, this._ModuleID);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Role", SqlDbType.NVarChar, this._Role);
            db.AddInParameter(dbCommand, "@Email", SqlDbType.NVarChar, this._Email);
            db.AddInParameter(dbCommand, "@Contact1", SqlDbType.NVarChar, this._Contact1);
            db.AddInParameter(dbCommand, "@Contact2", SqlDbType.NVarChar, this._Contact2);
            db.AddInParameter(dbCommand, "@Fax", SqlDbType.NVarChar, this._Fax);
            db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, this._Address);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);
            db.AddInParameter(dbCommand, "@CreationTime", SqlDbType.DateTime, this._CreationTime);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int moduleID, string name, string role, string email, string contact1, string contact2, string fax, string address, string createdBy, DateTime creationTime)
        {
            Item entity = new Item();
            entity.ID = iD;
            entity.ModuleID = moduleID;
            entity.Name = name;
            entity.Role = role;
            entity.Email = email;
            entity.Contact1 = contact1;
            entity.Contact2 = contact2;
            entity.Fax = fax;
            entity.Address = address;
            entity.CreatedBy = createdBy;
            entity.CreationTime = creationTime;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_CONTACT_Item_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.Int, this._ModuleID);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Role", SqlDbType.NVarChar, this._Role);
            db.AddInParameter(dbCommand, "@Email", SqlDbType.NVarChar, this._Email);
            db.AddInParameter(dbCommand, "@Contact1", SqlDbType.NVarChar, this._Contact1);
            db.AddInParameter(dbCommand, "@Contact2", SqlDbType.NVarChar, this._Contact2);
            db.AddInParameter(dbCommand, "@Fax", SqlDbType.NVarChar, this._Fax);
            db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, this._Address);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);
            db.AddInParameter(dbCommand, "@CreationTime", SqlDbType.DateTime, this._CreationTime);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Item entity = new Item();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_CONTACT_Item_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion
    }
}